Skip to content

[C2y] Claim conformance to WG14 N3254 #97581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2024

Conversation

AaronBallman
Copy link
Collaborator

The test coverage for this has to lean on LLVM's test coverage for the actual TBAA behavior, but this demonstrates that Clang emits reasonable LLVM IR to support this construct. It would be surprising should LLVM break this pattern given how ubiquitous the pattern is in the wild.

The paper can be found at: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3254.pdf

The test coverage for this has to lean on LLVM's test coverage for the
actual TBAA behavior, but this demonstrates that Clang emits reasonable
LLVM IR to support this construct. It would be surprising should LLVM
break this pattern given how ubiquitous the pattern is in the wild.
@AaronBallman AaronBallman added clang:frontend Language frontend issues, e.g. anything involving "Sema" c2y labels Jul 3, 2024
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jul 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2024

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

The test coverage for this has to lean on LLVM's test coverage for the actual TBAA behavior, but this demonstrates that Clang emits reasonable LLVM IR to support this construct. It would be surprising should LLVM break this pattern given how ubiquitous the pattern is in the wild.

The paper can be found at: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3254.pdf


Full diff: https://github.com/llvm/llvm-project/pull/97581.diff

2 Files Affected:

  • (added) clang/test/C/C2y/n3254.c (+149)
  • (modified) clang/www/c_status.html (+1-1)
diff --git a/clang/test/C/C2y/n3254.c b/clang/test/C/C2y/n3254.c
new file mode 100644
index 0000000000000..e08659cf377aa
--- /dev/null
+++ b/clang/test/C/C2y/n3254.c
@@ -0,0 +1,149 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
+// RUN: %clang_cc1 -triple=x86_64 -std=c2y %s -emit-llvm -o - | FileCheck %s
+
+/* WG14 N3254: Yes
+ * Accessing byte arrays
+ *
+ * NB: this basically boils down to how LLVM handles TBAA, so the best we can
+ * do for a Clang test is to test that the LLVM IR we pass is reasonable and we
+ * presume that LLVM has the test coverage to ensure that this behavior isn't
+ * regressed.
+ */
+
+struct S {
+  int x;
+  char c;
+  float f;
+};
+
+#define DECL_BUFFER(Ty, Name) alignas(Ty) unsigned char Name[sizeof(Ty)]
+
+// CHECK-LABEL: define dso_local i32 @foo(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[BUFFER:%.*]] = alloca [12 x i8], align 1
+// CHECK-NEXT:    [[S_PTR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 1 [[BUFFER]], i8 0, i64 12, i1 false)
+// CHECK-NEXT:    [[ARRAYDECAY:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    store ptr [[ARRAYDECAY]], ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[X:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[TMP0]], i32 0, i32 0
+// CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr [[X]], align 4
+// CHECK-NEXT:    ret i32 [[TMP1]]
+//
+int foo() {
+  DECL_BUFFER(struct S, buffer) = {};
+  struct S *s_ptr = (struct S *)buffer;
+  return s_ptr->x;
+}
+
+// CHECK-LABEL: define dso_local signext i8 @bar(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[BUFFER:%.*]] = alloca [12 x i8], align 1
+// CHECK-NEXT:    [[ARRAYDECAY:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    [[C:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[ARRAYDECAY]], i32 0, i32 1
+// CHECK-NEXT:    store i8 97, ptr [[C]], align 1
+// CHECK-NEXT:    [[ARRAYDECAY1:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    [[C2:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[ARRAYDECAY1]], i32 0, i32 1
+// CHECK-NEXT:    [[TMP0:%.*]] = load i8, ptr [[C2]], align 1
+// CHECK-NEXT:    ret i8 [[TMP0]]
+//
+char bar() {
+  DECL_BUFFER(struct S, buffer);
+  ((struct S *)buffer)->c = 'a';
+  return ((struct S *)buffer)->c;
+}
+
+// CHECK-LABEL: define dso_local float @baz(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[BUFFER:%.*]] = alloca [12 x i8], align 1
+// CHECK-NEXT:    [[ARRAYDECAY:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    [[F:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[ARRAYDECAY]], i32 0, i32 2
+// CHECK-NEXT:    store float 3.000000e+00, ptr [[F]], align 1
+// CHECK-NEXT:    [[ARRAYDECAY1:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    [[F2:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[ARRAYDECAY1]], i32 0, i32 2
+// CHECK-NEXT:    [[TMP0:%.*]] = load float, ptr [[F2]], align 1
+// CHECK-NEXT:    ret float [[TMP0]]
+//
+float baz() {
+  DECL_BUFFER(struct S, buffer);
+  ((struct S *)buffer)->f = 3.0f;
+  return ((const struct S *)buffer)->f;
+}
+
+struct T {
+  DECL_BUFFER(struct S, buffer);
+};
+
+// CHECK-LABEL: define dso_local signext i8 @quux(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[T:%.*]] = alloca [[STRUCT_T:%.*]], align 1
+// CHECK-NEXT:    [[S_PTR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 1 [[T]], i8 0, i64 12, i1 false)
+// CHECK-NEXT:    [[BUFFER:%.*]] = getelementptr inbounds [[STRUCT_T]], ptr [[T]], i32 0, i32 0
+// CHECK-NEXT:    [[ARRAYDECAY:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    store ptr [[ARRAYDECAY]], ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[C:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[TMP0]], i32 0, i32 1
+// CHECK-NEXT:    [[TMP1:%.*]] = load i8, ptr [[C]], align 4
+// CHECK-NEXT:    ret i8 [[TMP1]]
+//
+char quux() {
+  struct T t = {};
+  struct S *s_ptr = (struct S *)t.buffer;
+  return s_ptr->c;
+}
+
+// CHECK-LABEL: define dso_local float @quibble(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[BUFFER:%.*]] = alloca [12 x i8], align 1
+// CHECK-NEXT:    [[T_PTR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    [[S_PTR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 1 [[BUFFER]], i8 0, i64 12, i1 false)
+// CHECK-NEXT:    [[ARRAYDECAY:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    store ptr [[ARRAYDECAY]], ptr [[T_PTR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[T_PTR]], align 8
+// CHECK-NEXT:    [[BUFFER1:%.*]] = getelementptr inbounds [[STRUCT_T:%.*]], ptr [[TMP0]], i32 0, i32 0
+// CHECK-NEXT:    [[ARRAYDECAY2:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER1]], i64 0, i64 0
+// CHECK-NEXT:    store ptr [[ARRAYDECAY2]], ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[TMP1:%.*]] = load ptr, ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[F:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[TMP1]], i32 0, i32 2
+// CHECK-NEXT:    [[TMP2:%.*]] = load float, ptr [[F]], align 4
+// CHECK-NEXT:    ret float [[TMP2]]
+//
+float quibble() {
+  DECL_BUFFER(struct T, buffer) = {};
+  const struct T *t_ptr = (struct T *)buffer;
+  const struct S *s_ptr = (struct S *)t_ptr->buffer;
+  return s_ptr->f;
+}
+
+// CHECK-LABEL: define dso_local i32 @quorble(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[BUFFER:%.*]] = alloca [12 x i8], align 1
+// CHECK-NEXT:    [[S_PTR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    [[ARRAYDECAY:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    [[BUFFER1:%.*]] = getelementptr inbounds [[STRUCT_T:%.*]], ptr [[ARRAYDECAY]], i32 0, i32 0
+// CHECK-NEXT:    [[ARRAYDECAY2:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER1]], i64 0, i64 0
+// CHECK-NEXT:    [[X:%.*]] = getelementptr inbounds [[STRUCT_S:%.*]], ptr [[ARRAYDECAY2]], i32 0, i32 0
+// CHECK-NEXT:    store i32 12, ptr [[X]], align 1
+// CHECK-NEXT:    [[ARRAYDECAY3:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER]], i64 0, i64 0
+// CHECK-NEXT:    [[BUFFER4:%.*]] = getelementptr inbounds [[STRUCT_T]], ptr [[ARRAYDECAY3]], i32 0, i32 0
+// CHECK-NEXT:    [[ARRAYDECAY5:%.*]] = getelementptr inbounds [12 x i8], ptr [[BUFFER4]], i64 0, i64 0
+// CHECK-NEXT:    store ptr [[ARRAYDECAY5]], ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[S_PTR]], align 8
+// CHECK-NEXT:    [[X6:%.*]] = getelementptr inbounds [[STRUCT_S]], ptr [[TMP0]], i32 0, i32 0
+// CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr [[X6]], align 4
+// CHECK-NEXT:    ret i32 [[TMP1]]
+//
+int quorble() {
+  DECL_BUFFER(struct T, buffer);
+  ((struct S *)((struct T *)buffer)->buffer)->x = 12;
+  const struct S *s_ptr = (struct S *)((struct T *)buffer)->buffer;
+  return s_ptr->x;
+}
diff --git a/clang/www/c_status.html b/clang/www/c_status.html
index 3fb1efc1989e8..2030e1e62a3f4 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -1260,7 +1260,7 @@ <h2 id="c2y">C2y implementation status</h2>
     <tr>
       <td>Accessing byte arrays</td>
       <td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3254.pdf">N3254</a></td>
-      <td class="unknown" align="center">Unknown</td>
+      <td class="full" align="center">Yes</td>
     </tr>
     <tr>
       <td>Slay some earthly demons I</td>

@rjmccall
Copy link
Contributor

rjmccall commented Jul 3, 2024

Yes, I agree that we can reasonably claim to support this. This is something we would've seen ourselves as practically constrained from exploiting anyway, effectively making it well-defined even if the standard didn't act.

@AaronBallman AaronBallman merged commit ba1d21c into llvm:main Jul 8, 2024
11 checks passed
@AaronBallman AaronBallman deleted the aballman/wg14_n3254 branch July 8, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c2y clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants